package com.faxsun.controller.uc.entity;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadleafcommerce.profile.core.domain.Customer;
import org.broadleafcommerce.profile.core.service.CustomerService;
import org.springframework.stereotype.Component;

import com.faxsun.profile.core.service.FSThirdPartyCustomerService;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;

@Component
public class QQ extends ThirdPartyLoginAbs {
	private static final Log log = LogFactory.getLog(QQ.class);
	
	@Resource(name = "fsThirdPartyService")
	ThirdPartyLoginServer loginServer;

	@Resource(name = "blCustomerService")
	CustomerService customerService;
	
	@Resource(name = "blThirdPartyCustomerService")
	FSThirdPartyCustomerService fscustomer3thService;


	@Override
	public void ResponseURL(HttpServletRequest request, HttpServletResponse response)
	{
		Oauth oauth = new Oauth();
		try {
			String url=oauth.getAuthorizeURL(request);
			response.sendRedirect(url);
		} catch (QQConnectException e) {
			log.error("QQ Connect getAuthorizeURL  error ", e);
			e.printStackTrace();
		} catch (IOException e) {
			log.error("QQ response sendRedirect  error ", e);
			e.printStackTrace();
		}
	}

	@Override
	public String login(ThirdPartyObj obj) {
		if(obj.getUid() == null || obj.getUid().isEmpty()){
			return HOME;          // Auth Login Failed!   Inform, HOME---> FAIL Page
		}
		if(fscustomer3thService.checkCustomerBy3thUid(obj.getUid())){	
			Customer customer = fscustomer3thService.getCustomerByUid(obj.getUid());
			loginServer.loginDirectly(customer);
			return HOME;
		}else{
			return loginServer.registerThenLogin(obj.getNickName(),
					obj.getUid(), obj.getChannelId(),obj.getAvatar(),null);
		}
	}

	@Override
	public ThirdPartyObj get3rdObj(HttpServletRequest request, HttpServletResponse response, String channel, String path) {
		ThirdPartyObj obj3rd = new ThirdPartyObj();
		obj3rd.setChannelId(channel);
		AccessToken accessTokenObj;
		try {
			accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
			if (accessTokenObj.getAccessToken().equals("")) {
			//我们的网站被CSRF攻击了或者用户取消了授权
			//做一些数据统计工作
			//System.out.print(“没有获取到响应参数”);
		    log.info("授权被取消");
			}else {
				obj3rd.setAccessToken( accessTokenObj.getAccessToken());
				//accessToken = accessTokenObj.getAccessToken();
				OpenID openIDObj =  new OpenID(obj3rd.getAccessToken());
				obj3rd.setUid(openIDObj.getUserOpenID());
				// uid = openIDObj.getUserOpenID();
				UserInfo qzoneUserInfo = new UserInfo(obj3rd.getAccessToken(), obj3rd.getUid());
			    UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
			    if (userInfoBean.getRet() == 0) {
			    	obj3rd.setNickName(userInfoBean.getNickname());
			    }			
			}
		} catch (QQConnectException e) {
			log.error("QQ Connect access token  error ", e);
			e.printStackTrace();
		}
		return null;
	}

}
